package com.yuan.farmerwork.ynblog.rest;


import com.yuan.farmerwork.ynblog.domain.YnDocument;
import com.yuan.farmerwork.ynblog.domain.YnDocumentDetails;
import com.yuan.farmerwork.ynblog.domain.pojo.Blogs;
import com.yuan.farmerwork.ynblog.domain.pojo.DocCatalogs;
import com.yuan.farmerwork.ynblog.service.*;
import com.yuan.farmerwork.ynblog.utils.constants.PageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文档及项目说明前端控制器.
 *
 * @author yjs
 * @date 2020/10/8 15:52
 */
@Controller
public class DocumentController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private YnDocumentService documentService;

    @Autowired
    private YnDocumentDetailsService documentDetailsService;

    /**
     * 文档及项目说明页面.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/documents")
    public ModelAndView documents(Map map) {

        //文档数据获取
        List<YnDocument> documentList = documentService.list();
        documentList = documentList.stream().filter(x->x.getIsDelete()==0)
                .collect(Collectors.toList());
        map.put(PageConstants.PAGE_PARAM_DOCUMENT_LIST, documentList);
        //最新文章数据
        List<Blogs> blogLaterList = blogService.findBlogLaterList();
        map.put(PageConstants.PAGE_PARAM_BLOG_LATER_LIST, blogLaterList);
        //阅读多文章数据
        List<Blogs> readMoreList = blogService.findReadMoreList();
        map.put(PageConstants.PAGE_PARAM_READ_MORE_LIST, readMoreList);
        return new ModelAndView(PageConstants.PAGE_DOCUMENTS, map);
    }

    /**
     * 文档详情页面.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/doc/{id}")
    public ModelAndView docDtails(@PathVariable Long id, Map map) {
        //获取目录
        List<DocCatalogs> docCatalogs = documentDetailsService.findDocCatalogById(id, 0L);
        map.put(PageConstants.PAGE_PARAM_DOC_CATALOGS, docCatalogs);
        if (!docCatalogs.isEmpty()) {
            //获取内容
            YnDocumentDetails detailsContent = documentDetailsService.getById(docCatalogs.get(0).getId());
            map.put(PageConstants.PAGE_PARAM_DETAILS_CONTENT, detailsContent);
        }
        return new ModelAndView(PageConstants.PAGE_DOC, map);
    }

    /**
     * 文档详情页面.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/doc/{docId}/{id}")
    public ModelAndView docDtails(@PathVariable Long docId, @PathVariable Long id, Map map) {
        //获取内容
        Long parentId = 0L;
        YnDocumentDetails detailsContent = documentDetailsService.getById(id);
        if (detailsContent != null) {
            map.put(PageConstants.PAGE_PARAM_DETAILS_CONTENT, detailsContent);
            parentId = detailsContent.getParentId();
        }
        //获取目录
        List<DocCatalogs> docCatalogs = documentDetailsService.findDocCatalogById(docId, parentId);
        map.put(PageConstants.PAGE_PARAM_DOC_CATALOGS, docCatalogs);

        return new ModelAndView(PageConstants.PAGE_DOC, map);
    }
}
